package com.gxa.modules.payment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.PageUtils;
import com.gxa.modules.payment.entity.PayItem;
import com.gxa.modules.payment.form.PayItemForm;
import com.gxa.modules.payment.service.PayItemService;
import com.gxa.modules.payment.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "收费项目管理 接口")
@RestController
public class PayItemController {

    @Autowired
    private PayItemService payItemService;

    @ApiOperation("收费项目管理 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前第几页"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少数据"),
            @ApiImplicitParam(paramType = "query",name = "method", value = "查询条件 收费方式(21单次收费,22仪表收费,23周期收费)"),
            @ApiImplicitParam(paramType = "query",name = "service", value = "查询条件 收费项目(11电费,12物业费,13燃气费)"),
    })
    @GetMapping("/payItem/list")
    public Result<List<PayItem>> listResult(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<PayItem>> r = Result.success();

        String page1 = (String) params.get("page");
        String limit1 = (String) params.get("limit");

        Integer page = Integer.parseInt(page1);
        Integer limit = Integer.parseInt(limit1);

        try{
            PageHelper.startPage(page,limit);
            List<PayItem> payItems = this.payItemService.queryByPage(params);
            PageInfo<PayItem> pageInfo = new PageInfo<>(payItems);

            long total = (int)pageInfo.getTotal();
          return  Result.success(payItems,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }


    @ApiOperation("收费项目管理 添加")
    @PostMapping("/payItem/add")
    public com.gxa.common.utils.Result payItemAdd(@RequestBody PayItemForm payItemForm){
        System.out.println(payItemForm);
        com.gxa.common.utils.Result r = new com.gxa.common.utils.Result<>().error();

        try{
            this.payItemService.payItemAdd(payItemForm);
            r = new com.gxa.common.utils.Result().ok();
        } catch (Exception e){

            e.printStackTrace();
        }
        return r;
    }


    @ApiOperation("收费项目管理 修改前查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "id", value = "查询条件 id"),
    })
    @GetMapping("/payItem/queryById")
    public Result<List<PayItem>> queryById(@RequestParam Integer id){
        Result<List<PayItem>> r = Result.success();

        try{
            List<PayItem> payItems = this.payItemService.queryAll(id);

            PageInfo<PayItem> pageInfo = new PageInfo<>(payItems);

            long total = (int)pageInfo.getTotal();
            return  Result.success(payItems,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("收费项目管理 修改")
    @PutMapping("/payItem/update")
    public Result payItemUpdate(@RequestBody PayItemForm payItemForm){
        System.out.println(payItemForm);
        Result r = new Result<>();

        try{
            this.payItemService.payItemUpdate(payItemForm);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("收费项目管理 删除")
    @DeleteMapping("/payItem/delete")
    public Result payItemDelete(@RequestParam Integer id){
        Result r = new Result<>();

        try{
            this.payItemService.payItemDalete(id);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("收费项目管理 删除多条")
    @DeleteMapping("/payItem/ids/delete")
    public Result payItemDeletes(@RequestBody List<Integer> ids){
        Result r = Result.success();

        try{
            this.payItemService.payItemDaletes(ids);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
}
